package org.paradise.dms.web.action.aptview;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.poi.NeoPoiUtil;
import org.paradise.dms.services.DormitoryService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * Description: 给出房间状态查看视图
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-18 上午01:53:25 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "dormitoryStatusViewAction")
public class DormitoryStatusViewAction extends DMSBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7231842751788618617L;

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(DormitoryStatusViewAction.class);
	@Autowired
	private DormitoryService dormitoryService;
	@Autowired
	private PagerService pagerService;
	private Pager pager;
	protected String currentPage;
	@SuppressWarnings("unchecked")
	private List myDormInfoList;

	@SuppressWarnings("unchecked")
	public List getMyDormInfoList() {
		return myDormInfoList;
	}

	@SuppressWarnings("unchecked")
	public void setMyDormInfoList(List myDormInfoList) {
		this.myDormInfoList = myDormInfoList;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

	protected String totalRows;
	protected String pagerMethod;

	public DormitoryService getDormitoryService() {
		return dormitoryService;
	}

	public void setDormitoryService(DormitoryService dormitoryService) {
		this.dormitoryService = dormitoryService;
	}

	private String apartmentid;
	private String dormitoryfloorno;

	public String getApartmentid() {
		return apartmentid;
	}

	public void setApartmentid(String apartmentid) {
		this.apartmentid = apartmentid;
	}

	public String getDormitoryfloorno() {
		return dormitoryfloorno;
	}

	public void setDormitoryfloorno(String dormitoryfloorno) {
		this.dormitoryfloorno = dormitoryfloorno;
	}

	/**
	 * 
	 * Description: 列出所有空闲的宿舍信息
	 * 
	 * @Version1.0 2009-3-18 上午01:54:34 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	/*
	public String listAvailableDormitoryByApartmentId() {
		int totalRow = dormitoryService.getTotalAvailDormRows(this.getApartmentid(), this.getDormitoryfloorno());
		pager = pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), totalRow);
		this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
		this.setTotalRows(String.valueOf(totalRow));
		myDormInfoList = dormitoryService.listAvailDormitoryByApartmentid(getApartmentid(), this.getDormitoryfloorno(), pager.getPageSize(), pager.getStartRow());
		if (myDormInfoList != null & myDormInfoList.size() > 0) {
			// 测试数据
			// for (int k = 0; k < myDormInfoList.size(); k++) {
			// Object[] o = (Object[]) myDormInfoList.get(k);
			// int dormitoryid = (Integer) o[0];
			// int inAmount = (Integer) o[1];
			// String dormitoryname = (String) o[5];
			// int dormitoryroomtype = (Integer) o[3];
			// int dormitorysex = (Integer) o[4];
			// log.info("dormitoryid:\t" + dormitoryid + "\t" + "inAmount\t" +
			// inAmount);
			// log.info("dormitoryname:\t" + dormitoryname + "\t" +
			// "dormitoryroomtype\t" + dormitoryroomtype + "dormitorysex\t" +
			// dormitorysex);
			// }

			return SUCCESS;
		}
		return INPUT;
	}
*/
	/**
	 * 
	 * Description: 根据公寓id查询该公寓所有供宿舍卫生评比的宿舍excel
	 * 
	 * @Version1.0 May 5, 2009 3:04:39 PM 李双江（paradise.lsj@gmail.com）创建
	 */
	@SuppressWarnings( { "unchecked", "deprecation" })
	public String exportDormrateExcelByApartId() {
		List flist = dormitoryService.getDormListForRatingExcelByApartId(this.getApartmentid());
		List list = new ArrayList();
		for (int i = 0; i < flist.size(); i++) {
			Object[] obj = new Object[3];
			obj[0] = ((Object[]) flist.get(i))[0];
			obj[1] = ((Object[]) flist.get(i))[1];
			//log.info(obj[0]); // dormitoryid =="234"
			//log.info(obj[1]); // dormitorydisplayname =="一号楼1层101宿舍"
			obj[2] = new Date().toLocaleString();
			list.add(obj);
		}

		// HttpServletResponse response =
		// WebContextFactory.get().getHttpServletResponse();
		HttpServletResponse response = ServletActionContext.getResponse();
		String fname = "DormitoryRatingList";// Excel文件名
		OutputStream os;
		try {
			String[] titles = { "ID", "宿舍", "检查时间", "分数", "检查人" };

			os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename=" + fname + ".xls");
			// 设定输出文件头,该方法有两个参数，分别表示应答头的名字和值。
			response.setContentType("application/msexcel");
			// PoiUtil poiUtil = new PoiUtil();
			// poiUtil.poiWriteExcel(chargeFeesRowValues, listForExport, os);
			NeoPoiUtil.poiWriteExcel(titles, list, os);
			return SUCCESS;
		} catch (IOException e) {
			log.error("DMS_error:无法取到OutputStream");
			log.error("DMS_error:错误原因" + e);
			return INPUT;
		}
	}

	/**
	 * 
	 * Description: 列出所有在维修的宿舍状态
	 * 
	 * @Version1.0 2009-3-18 上午01:57:52 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public String listAllDormitoryInRepair() {
		return SUCCESS;
	}

	/**
	 * 
	 * Description: 根据学号查询宿舍卫生状况
	 * 
	 * @Version1.0 May 9, 2009 6:38:39 PM 郑旭（zhengxu2006@gmail.com）创建
	 * studentdormitorytype对于dormitory表的dormitoryconcretefuncid
	 * @param stuno
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String[][] getDormitoryRateStatus(String studentno,int studentdormitorytype) {
		List l = dormitoryService.getDormitoryRateByStudentno(studentno,studentdormitorytype);
		if (null != l && l.size() > 0) {
			String[][] sarr = new String[l.size()][6];
			Object[] o = new Object[6];
			for (int a = 0; a < l.size(); a++) {
				o = (Object[]) l.get(a);
				for (int b = 0; b < 6; b++) {
					if (null != o[b]) { 
						if(b == 3){
							String t = ((Date) o[3]).toString();
							sarr[a][3] = (t.length()>=10?t.substring(0, 10):"");
						} else{
							sarr[a][b] = o[b].toString();
						}
					}else{
						sarr[a][b] = "";
					}
				}
			}
			
			return sarr;
		}
		return null;
	}

}
